package com.lq.demo.demo.mode.exp;

import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;


/**
 * jdk的动态代理演示sample demo
 */
@Slf4j
public class EatProxy implements InvocationHandler {

    private Object iEat;


    public Object bind(Object iEat) {
        log.info("bind");
        this.iEat = iEat;
        IEat proxy = (IEat) Proxy.newProxyInstance(iEat.getClass().getClassLoader(), iEat.getClass().getInterfaces(), this);
        return proxy;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        log.info("proxy  invoke");
        //可以修改参数等来增强方法
        method.invoke(iEat, args[0] + "梨子");
        return null;
    }
}
